<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;

/**
 * App\Models\Topic
 *
 * @property int $id
 * @property string $title 帖子标题
 * @property string $content 帖子内容
 * @property int $user_id po主
 * @property int $category_id 分类id
 * @property int $reply_count 评论数量
 * @property int $view_count 浏览数量
 * @property int $collect_count 收藏数量
 * @property int $like_count 点赞数量
 * @property int $last_reply_user_id 最后评论用户id
 * @property int $order 排序
 * @property string|null $desc seo内容描述
 * @property string|null $slug seo引擎优化
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Database\Factories\TopicFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Topic newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic query()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereCollectCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereLastReplyUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereLikeCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereReplyCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Topic whereViewCount($value)
 * @property-read \App\Models\Category|null $category
 * @property-read \App\Models\User|null $user
 * @method static \Illuminate\Database\Eloquent\Builder|Topic recent()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic recentReply()
 * @method static \Illuminate\Database\Eloquent\Builder|Topic topicSelect($order = 'reply')
 * @mixin \Eloquent
 */
class Topic extends Model
{
    use HasFactory,Notifiable;

    protected $fillable = ['title','content','slug','desc','category_id'];


    public function user(){
        return $this->belongsTo(User::class);
    }

    public function category(){
        return $this->belongsTo(Category::class);
    }

    public function replies(){
        return $this->hasMany(Reply::class);
    }

    /*
     * 获取seo后的帖子链接
     * */
    public function link($param=[]){
        return route('topics.show',array_merge([$this->id,$this->slug],$param));
    }

    /*
     * 最新创建的帖子
     * */
    public function scopeRecent($query){
        return $query->orderBy('created_at','desc');
    }

    /*
     * 最新回复的帖子
     * */
    public function scopeRecentReply($query){
        return $query->orderBy('updated_at','desc');
    }

    public function scopeTopicSelect($query,$order='reply'){

        switch ($order){
            default:
            case 'reply'://最近评论
                return $query->recentReply();
            case 'post'://最新帖子
                return $query->recent();
        }

    }

}
